package com.walk.zworld.ui.base;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.walk.zworld.api.http.JumpInterceptor;
import com.walk.zworld.api.utils.ListUtil;

import java.util.List;

public abstract class BaseMutiRecyclerAdapter<T extends MultiItemEntity, K  extends BaseViewHolder> extends BaseMultiItemQuickAdapter<T, K> {
    protected View.OnClickListener mOnClickListener;
    protected OnItemChildClickListener2<T> mOnItemChildClickListener2;
    public BaseMutiRecyclerAdapter(List<T> data) {
        super(data);
        mOnClickListener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer integer= JumpInterceptor.getTag(v,Integer.class);
                if(integer==null||!JumpInterceptor.canClick()){
                    return;
                }
                int headCount=getHeaderLayoutCount();
                int position=integer-headCount;
                T t=getItem(position);
                if(mOnItemChildClickListener2!=null){
                    mOnItemChildClickListener2.onItemClick(position,t,v);
                }
            }
        };
    }

    public void setData(List<T> data) {
        setNewData(data);
        notifyDataSetChanged();
    }


    public void appendData(List<T> data) {
        if(getData()!=null){
            addData(data);
        }else{
            setData(data);
        }
    }

    public void appendData(int index, List<T> data) {
        if(data!=null){
            addData(index,data);
        }else{
            setData(data);
        }
    }


    public T getLastData(){
        return ListUtil.haveData(getData()) ?getData().get(getData().size()-1):null;
    }

    public int size(){
        return getData()==null?0:getData().size();
    }

    public List<T> getArray() {
        return getData();
    }

    public RecyclerView.Adapter returnRecyclerAdapter() {
        return this;
    }

    public void notifyReclyDataChange() {
        notifyDataSetChanged();
    }

    public void setOnItemChildClickListener2(OnItemChildClickListener2<T> onItemChildClickListener2) {
        mOnItemChildClickListener2 = onItemChildClickListener2;
    }

    public interface OnItemChildClickListener2<T>{
        public void onItemClick(int position,T t,View view);
    }

    public interface DataAdapter<E>{
        public void setData(List<E> data);
        public void appendData(List<E> data);
        public void appendData(int index,List<E> data);
        public List<E> getArray();
        public RecyclerView.Adapter returnRecyclerAdapter();
        public void notifyReclyDataChange();
    }
}